import config
import os
import re
import shutil

current_path = os.getcwd()  # 获取当前工作目录


def replace_namespace():
    deploy_demo_path = current_path + '/k8s-yaml/deployment-demo'  # 获取下级目录
    deploy_path = current_path + '/k8s-yaml/deployment'  # 获取下级目录
    svc_path = current_path + '/k8s-yaml/service'  # 获取下级目录
    configmap_path = current_path + '/k8s-yaml/configmap'  # 获取下级目录
    secret_path = current_path + '/k8s-yaml/secret'  # 获取下级目录
    ingress_path = current_path + '/k8s-yaml/ingress'  # 获取下级目录
    nacos_path = current_path + '/k8s-yaml/nacos'  # 获取下级目录
    xxljob_path = current_path + '/k8s-yaml/xxljob'  # 获取下级目录
    configmap_file_names = os.listdir(configmap_path)
    secret_file_names = os.listdir(secret_path)
    ingress_file_names = os.listdir(ingress_path)
    nacos_file_names = os.listdir(nacos_path)
    xxljob_file_names = os.listdir(xxljob_path)

    for configmap_name in configmap_file_names:
        with open(configmap_path + '/{}'.format(configmap_name), 'r') as f:
            content = f.read()
            content = content.replace('k8s_namespace_demo', config.k8s_namespace)

        with open(configmap_path + '/{}'.format(configmap_name), 'w') as f:
            f.write(content)

    for secret_name in secret_file_names:
        with open(secret_path + '/{}'.format(secret_name), 'r') as f:
            content = f.read()
            content = content.replace('k8s_namespace_demo', config.k8s_namespace)

        with open(secret_path + '/{}'.format(secret_name), 'w') as f:
            f.write(content)

    for ingress_name in ingress_file_names:
        with open(ingress_path + '/{}'.format(ingress_name), 'r') as f:
            content = f.read()
            content = content.replace('k8s_namespace_demo', config.k8s_namespace).replace('cem_name',
                                                                                          config.config.get(
                                                                                              'cem_name')).replace(
                'cem_demo', config.config.get('cem_com')).replace('ingress_address',
                                                                  config.ingress_address).replace('opms_demo',
                                                                                                  config.config.get(
                                                                                                      'opms_com')).replace(
                'opms_name', config.config.get('opms_name'))
        with open(ingress_path + '/{}'.format(ingress_name), 'w') as f:
            f.write(content)

    for nacos_name in nacos_file_names:
        with open(nacos_path + '/{}'.format(nacos_name), 'r') as f:
            content = f.read()
            content = content.replace('nacos_db', config.nacos_db).replace('k8s_namespace_demo',config.k8s_namespace).replace('mysql_host',
                                                                                                 config.config.get('mysql_host')).replace('mysql_port',
                                                                                                 config.config.get('mysql_port')).replace('mysql_user',
                                                                                                 config.config.get('lfll_prod')). replace('mysql_password',config.config.get('mysql_password'))


        with open(nacos_path + '/{}'.format(nacos_name), 'w') as f:
            f.write(content)

    for xxljob_name in xxljob_file_names:
        with open(xxljob_path + '/{}'.format(xxljob_name), 'r') as f:
            content = f.read()
            content = content.replace('xxljob_db', config.xxljob_db).replace('k8s_namespace_demo',config.k8s_namespace
                                                                             ).replace('mysql_host',
                                                                                                 config.config.get('mysql_host')).replace('mysql_port',
                                                                                                 config.config.get('mysql_port')).replace('mysql_user',
                                                                                                 config.config.get('lfll_prod')).replace('mysql_password',
                                                                                                 config.config.get('mysql_password'))
        with open(xxljob_path + '/{}'.format(xxljob_name), 'w') as f:
            f.write(content)

    names = list(config.name_str.split(','))

    for name in names:
        if 'web' in name:
            with open(deploy_path + '/{}-deploy.yaml'.format(name), 'w') as f:
                with open(deploy_demo_path + '/dia-web-tenants-deploy.yaml', 'r') as dia_web_tenants_file:
                    image = config.harbor_addr + 'name' + ':' + config.image_version
                    f.write(dia_web_tenants_file.read().replace('dia-web-tenants', name).replace('k8s_namespace_demo',
                                                                                                 config.k8s_namespace).replace('image_demo',
                                                                                                 image))

            with open(svc_path + '/{}-svc.yaml'.format(name), 'w') as f:
                with open(deploy_demo_path + '/dia-web-tenants-svc.yaml', 'r') as dia_web_svc_file:
                    f.write(dia_web_svc_file.read().replace('dia-web-tenants', name).replace('k8s_namespace_demo',
                                                                                             config.k8s_namespace))
        else:
            with open(deploy_path + '/{}-deploy.yaml'.format(name), 'w') as f:
                with open(deploy_demo_path + '/dia-gateway-deploy.yaml', 'r') as dia_gateway_file:
                    image = config.harbor_addr + 'name' + ':' + config.image_version
                    f.write(dia_gateway_file.read().replace('dia-gateway', name).replace('k8s_namespace_demo',
                                                                                         config.k8s_namespace)
                            .replace('env_demo', config.app_env).replace('nacos_namespace_demo',
                                                                         config.nacos_namespace).replace('nacos_demo',
                                                                                                         config.nacos_addr).replace('image_demo',
                                                                                                 image))
            with open(svc_path + '/{}-svc.yaml'.format(name), 'w') as f:
                with open(deploy_demo_path + '/dia-gateway-svc.yaml', 'r') as dia_gateway_svc_file:
                    f.write(dia_gateway_svc_file.read().replace('dia-gateway', name).replace('k8s_namespace_demo',
                                                                                             config.k8s_namespace))

def replace_in_file(file_path, pattern, replacement):
    with open(file_path, 'r') as file:
        content = file.read()
    content = re.sub(pattern, replacement, content)
    with open(file_path, 'w') as file:
        file.write(content)


def main():
    # shutil.copytree("DIA", "DIA.bak")

    # 替换变量
    for key, value in config.config.items():
        pattern = f'{key}_nacos'
        replacement = str(value)
        for root, dirs, files in os.walk("DIA"):
            for file in files:
                if file.endswith(".yml"):
                    replace_in_file(os.path.join(root, file), pattern, replacement)


if __name__ == "__main__":
    replace_namespace()
    main()